package com.zhangxiaofanfan.hotcode.fourth.third;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * TODO
 *
 * @author zhangxiaofanfan
 * @date 2024-06-26 20:33:33
 */
public class HotCode058 {
    public static void main(String[] args) {
        HotCode058 hotCode058 = new HotCode058();
        int[] candidates = {2, 3, 6, 7};
        hotCode058.combinationSum(candidates, 7).forEach(System.out::println);
    }

    private List<List<Integer>> result = new ArrayList<>();
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        Arrays.sort(candidates);
        bfs(candidates, target, 0, new ArrayList<>());
        return result;
    }

    public void bfs(int[] candidates, int target, int begin, List<Integer> list) {
        if (target < 0) {
            return;
        }
        if (target == 0) {
            result.add(new ArrayList<>(list));
        }
        for (int i = begin; i < candidates.length; i++) {
            list.addLast(candidates[i]);
            bfs(candidates, target - candidates[i], i, list);
            list.removeLast();
        }
    }
}
